/*
 ------------------------------------------------------------------------------
	winmad++ -	a test program for using SDL and mad library on Windows
				with object oriented UI style
				with low level mad (seek)

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "precomp.h"

int getID3(const char* lpszFile)
{
	struct id3_file* id3f;

	/* get ID3 tags */
	id3f = id3_file_open(lpszFile, ID3_FILE_MODE_READONLY);
	if (id3f == 0)
	{
		/* __TO_DO
			Not an error, mp3 might not have ID3 tag */
		fprintf(stderr, "Unable to open id3: %s !\n", lpszFile);
		return -1;
	}
	id3_tag* tag = id3_file_tag(id3f);
	printf("id3 tag version: %x\n", id3_tag_version(tag));
	for (unsigned int i=0; i < tag->nframes; i++)
	{
		id3_frame* id3fr = tag->frames[i];
		printf("Frame #%d: \n\tid: %s\n\tdescription: %s\n\tencoded: %d\n\tfields: %d\n", i, id3fr->id, id3fr->description, (int)id3fr->encoded_length, id3fr->nfields);
		for (unsigned int j=0; j < id3fr->nfields; j++)
		{
			id3_field* id3fi = id3_frame_field(id3fr, j);
			printf("\tField #%d: \n", j);
			
			enum id3_field_type type = id3_field_type(id3fi);
			printf("\t\tType: %d\n", type);
			id3_field_textencoding enc;
			unsigned int num;
			id3_length_t len;
			const id3_byte_t* pData;
			switch (type)
			{
				case ID3_FIELD_TYPE_TEXTENCODING:
					enc = id3_field_gettextencoding(id3fi);
					printf("\t\tText encoding: %d\n", enc);
					break;

				case ID3_FIELD_TYPE_STRINGLIST:
					num = id3_field_getnstrings(id3fi);
					printf("\t\tString list (%d):\n", num);
					for (unsigned int k=0; k < num; k++)
					{
						printf("\t\t\t\"%s\"\n", id3_ucs4_latin1duplicate(id3_field_getstrings(id3fi, k)));
					}
					break;

				case ID3_FIELD_TYPE_STRING:
					printf("\t\t\t\"%s\"\n", id3_ucs4_latin1duplicate(id3_field_getstring(id3fi)));
					break;

				case ID3_FIELD_TYPE_LATIN1:
					printf("\t\t\t\"%s\"\n", id3_field_getlatin1(id3fi));
					break;

				case ID3_FIELD_TYPE_INT8:
					printf("\t\t\t\"%x\"\n", (unsigned int)id3_field_getint(id3fi));
					break;

				case ID3_FIELD_TYPE_BINARYDATA:
				{
					FILE* fp = fopen("data.bin", "wb");
					pData = id3_field_getbinarydata(id3fi, &len);
					if (fp != NULL)
					{
						fwrite(pData, 1, len, fp);
						fclose(fp);
					}
					break;
				}
				
				case ID3_FIELD_TYPE_LATIN1FULL:
				case ID3_FIELD_TYPE_LATIN1LIST:
				case ID3_FIELD_TYPE_STRINGFULL:
				case ID3_FIELD_TYPE_LANGUAGE:
				case ID3_FIELD_TYPE_FRAMEID:
				case ID3_FIELD_TYPE_DATE:
				case ID3_FIELD_TYPE_INT16:
				case ID3_FIELD_TYPE_INT24:
				case ID3_FIELD_TYPE_INT32:
				case ID3_FIELD_TYPE_INT32PLUS:
					break;

			}

			//printf("\tField #%d: \n\t\tType: %x\n", j, id3_field_type(id3fi));
		}
	}

	return 0;
}